package com.lvyq.qrobot.util;

import java.util.Random;

/**
* @author lvyq
* @version 1.0
* Create by 2024-06-07 9:14
*/
public class LotteryUtils {
    private static final Random random = new Random();

    public static String calculatePrize(double[] probabilities, String[] prizes) {
        double totalProb = 0;
        for (double prob : probabilities) {
            totalProb += prob;
        }

        if (Math.abs(totalProb - 1) > 1e-6) {
            throw new IllegalArgumentException("Probabilities must sum up to 1");
        }

        double randomValue = random.nextDouble();
        double cumulativeProb = 0;

        for (int i = 0; i < probabilities.length; i++) {
            cumulativeProb += probabilities[i];
            if (randomValue <= cumulativeProb) {
                return prizes[i];
            }
        }

        throw new IllegalStateException("Should not reach here");
    }


    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            double[] probabilities={0.95,0.02,0.01,0.01,0.01};
            String[] prizes={"谢谢参与","紫翎羽","翠鸟羽","火凤羽","金翎羽"};
            String s = calculatePrize(probabilities, prizes);
            System.out.println(s);
        }

    }
}
